package mvp.com.register.presenter;

import android.os.Handler;
import mvp.com.register.model.IUserBiz;
import mvp.com.register.model.bean.User;
import mvp.com.register.model.impl.UserBiz;
import mvp.com.register.view.IRegisterView;

/**
 * Created by YoungerChen on 2016/9/13.
 */
public class UserRegisterPresenter {

    private IRegisterView mRegisterView;
    private IUserBiz biz;

    private Handler handler = new Handler();

    public UserRegisterPresenter(IRegisterView registerView) {
        this.mRegisterView = registerView;
        this.biz = new UserBiz();
    }

    public void register() {

        mRegisterView.showProgressBar();
        final User user = new User();
        user.setUsername(mRegisterView.getUsername());
        user.setPassword(mRegisterView.getPassword());

        biz.register(user, new IRegisterView.OnRegisterListener() {
            @Override
            public void registerSuccess(User user) {

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mRegisterView.toMainActivity();
                        mRegisterView.hideProgressBar();
                    }
                });
            }

            @Override
            public void registerFailed() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mRegisterView.showFailedError();
                        mRegisterView.hideProgressBar();
                    }
                });
            }
        });
    }


    public void clear() {
        mRegisterView.clearUsername();
        mRegisterView.clearPassword();
    }
}
